﻿using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Loginter
{
    public partial class TruckTeamInfo : System.Web.UI.Page
    {
        //BLL层车队用户
        TruckTeamBLL teambll = new TruckTeamBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRoleList();
                BindUserList();
            }
        }

        /// <summary>
        /// 绑定角色列表
        /// </summary>
        private void BindRoleList()
        {
            DataTable table = teambll.GetTruckList();
            ddl_name.DataSource = table;
            ddl_name.DataTextField = "TeamName";
            ddl_name.DataValueField = "TeamID";
            ddl_name.DataBind();

            ddl_name.Items.Insert(0, new ListItem("--请选择--", "0"));
        }
        private void BindUserList()
        {
            //姓名
            string Leader = txt_penplo.Text.Trim();
            //车队编号
            int TeamID = Convert.ToInt32(ddl_name.SelectedValue);
            //是否删除
            int isdelte = Convert.ToInt32(rblDelete.SelectedValue);
            DataTable table = teambll.GetTruckTeamList(TeamID, Leader, isdelte);
            if (table == null)
            {
                Console.Write("数据源为空!");
            }
            gr_TruckTeam.DataSource = table;
            gr_TruckTeam.DataBind();
        }
        /// <summary>
        /// 查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_select_Click(object sender, EventArgs e)
        {
            BindUserList();
        }

        /// <summary>
        /// 添加按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddTruckTeam.aspx");

        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gr_TruckTeam_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "del")
            {
                int userid = Convert.ToInt32(e.CommandArgument);
                int result = teambll.DeleteTruckTeam(userid);
                if (result == 1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除成功！')</script>");
                    BindUserList();
                }
                else if (result == -1)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('该信息已删除！')</script>");
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('删除失败！')</script>");
                }
            }
        }
    }
}